package com.baomidou.springmvc.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.springmvc.model.system.Role;
import com.baomidou.springmvc.service.system.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

/**
 * @author hemj
 */
@Controller
@RequestMapping("/role")
public class RoleController extends BaseController {

    private final IRoleService roleService;

    @Autowired
    public RoleController(IRoleService roleService) {
        this.roleService = roleService;
    }

    // 首页
    @RequestMapping("/")
    public ModelAndView index(ModelAndView modelAndView) {
        modelAndView.setViewName("role_index");
        modelAndView.addObject("roleList", roleService.selectList(null));
        return modelAndView;
    }

    // 测试内置单表分页查询
    @RequestMapping(value = "/buildinPage")
    public ModelAndView buildinPage(ModelAndView modelAndView,
                                    @RequestParam(name = "page", required = false, defaultValue = "1") int page,
                                    @RequestParam(name = "rows", required = false, defaultValue = "10") int rows,
                                    @RequestParam(name = "name", required = false, defaultValue = "") String name) {
        Page<Role> pageParam = new Page<>();
        pageParam.setOffset(Math.max(0, (page - 1) * rows));
        pageParam.setLimit(rows);
        pageParam.setCurrent(page);
        pageParam.setSize(rows);
        roleService.buildinSelectRolePage(pageParam, name);
        System.out.println("offset: " + pageParam.getOffset());
        System.out.println("limit: " + pageParam.getLimit());
        System.out.println("total: " + pageParam.getTotal());

        List<Role> list = pageParam.getRecords();
        modelAndView.setViewName("role_index");
        modelAndView.addObject("roleList", list);
        return modelAndView;
    }

    // 测试分页查询
    @RequestMapping(value = "/", params = {"page", "rows"})
    public ModelAndView indexPage(ModelAndView modelAndView, int page, int rows,
                                  @RequestParam(name = "name", required = false, defaultValue = "") String name) {
        Page<Role> pageParam = new Page<>();
        pageParam.setOffset(Math.max(0, (page - 1) * rows));
        pageParam.setLimit(rows);
        pageParam.setCurrent(page);
        pageParam.setSize(rows);
        roleService.selectRolePage(pageParam, name);
        System.out.println("offset: " + pageParam.getOffset());
        System.out.println("limit: " + pageParam.getLimit());
        System.out.println("total: " + pageParam.getTotal());

        List<Role> list = pageParam.getRecords();
        modelAndView.setViewName("role_index");
        modelAndView.addObject("roleList", list);
        return modelAndView;
    }

    // 测试分页查询（Map下划线自动转驼峰）
    @RequestMapping(value = "/maplist")
    public ModelAndView maplist(ModelAndView modelAndView,
                                @RequestParam(name = "page", required = false, defaultValue = "1") int page,
                                @RequestParam(name = "rows", required = false, defaultValue = "10") int rows,
                                @RequestParam(name = "name", required = false, defaultValue = "") String name) {
        Page<Map> pageParam = new Page<>();
        pageParam.setOffset(Math.max(0, (page - 1) * rows));
        pageParam.setLimit(rows);
        pageParam.setCurrent(page);
        pageParam.setSize(rows);
        roleService.selectMapList(pageParam, name);
        System.out.println("offset: " + pageParam.getOffset());
        System.out.println("limit: " + pageParam.getLimit());
        System.out.println("total: " + pageParam.getTotal());

        List<Map> list = pageParam.getRecords();
        modelAndView.setViewName("role_index");
        modelAndView.addObject("roleList", list);
        return modelAndView;
    }

    // 测试修改页面
    @RequestMapping("/preSave")
    public ModelAndView preSave(ModelAndView modelAndView,
                                @RequestParam(value = "id", required = false) String id) {
        modelAndView.setViewName("role_save");
        if (id != null) {
            modelAndView.addObject("role", roleService.selectById(id));
        }
        return modelAndView;
    }

    // 测试新增和修改操作
    @ResponseBody
    @RequestMapping("save")
    public Object save(Role role) {
        if (role.getRoleId() == null || role.getRoleId().isEmpty()) {
            role.setRoleId(String.valueOf(System.currentTimeMillis()));
            return roleService.insert(role) ? renderSuccess("添加成功") : renderError("添加失败");
        } else {
            return roleService.updateById(role) ? renderSuccess("修改成功") : renderError("修改失败");
        }
    }

    // 测试删除操作
    @ResponseBody
    @RequestMapping("/delete")
    public Object delete(@RequestParam(value = "id", required = false) String id) {
        return roleService.deleteById(id) ? renderSuccess("删除成功") : renderError("删除失败");
    }

}
